package edu.fdzc.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.fdzc.system.entity.QuestionType;
import edu.fdzc.system.exception.CustomException;
import edu.fdzc.system.mapper.QuestionTypeMapper;
import edu.fdzc.system.service.QuestionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionTypeServiceImpl implements QuestionTypeService {

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    @Override
    public void add(QuestionType questionType) {
        List<QuestionType> list = questionTypeMapper.selectByName(questionType.getName());
        if (CollectionUtil.isNotEmpty(list)) {
            throw new CustomException("-1", "题型名称不能重复");
        }
        questionTypeMapper.add(questionType);
    }

    @Override
    public void updateById(QuestionType questionType) {
        List<QuestionType> list = questionTypeMapper.selectByName(questionType.getName());
        if (CollectionUtil.isNotEmpty(list) && !questionType.getId().equals(list.get(0).getId())) {
            throw new CustomException("-1", "题型名称不能重复");
        }
        questionTypeMapper.updateById(questionType);
    }

    @Override
    public void deleteById(Integer id) {
        questionTypeMapper.deleteById(id);
    }

    @Override
    public void delectBatch(List<Integer> ids) {
        for(Integer id : ids){
            questionTypeMapper.deleteById(id);
        }
    }

    @Override
    public QuestionType selectById(Integer id) {
        return questionTypeMapper.selectById(id);
    }

    @Override
    public List<QuestionType> selectAll(QuestionType questionType) {
        return questionTypeMapper.selectAll(questionType);
    }

    @Override
    public PageInfo<QuestionType> selectPage(QuestionType questionType, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuestionType> list = questionTypeMapper.selectAll(questionType);
        return PageInfo.of(list);
    }
}
